package com.freddy.sqlite.utils;

import android.database.Cursor;
import android.text.TextUtils;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Freddy on 2015/12/8.
 * chenshichao@outlook.com
 * 基本类型类管理器
 */
public class BasicClass {

    /**
     * 基本类型类集合
     */
    private static Map<Class, Class> basicClassMap = new HashMap<Class, Class>();

    /**
     * 获得包装类
     * @param typeClass
     * @return
     */
    public static Class<? extends Object> getBasicClass(Class typeClass) {
        Class cls = basicClassMap.get(typeClass);
        if(null == cls) {
            cls = typeClass;
        }

        return cls;
    }

    /**
     * 判断是否基本类型类
     * @param typeClass
     * @return
     */
    public static boolean isBasicClassType(Class typeClass) {
        if(typeClass.equals(Integer.class) || typeClass.equals(Long.class)
                || typeClass.equals(Float.class)
                || typeClass.equals(Double.class)
                || typeClass.equals(Boolean.class)
                || typeClass.equals(Byte.class)
                || typeClass.equals(Short.class)
                || typeClass.equals(String.class)) {

            return true;
        }

        return false;
    }

    /**
     * 把值设置进类属性里
     * @param cursor
     * @param cls
     * @return
     * @throws Exception
     */
    public static Object setValuesToFields(Cursor cursor, Class cls) throws Exception {
        String[] columnNames = cursor.getColumnNames();// 字段数组
        Object obj = cls.newInstance();
        Field[] fields = cls.getDeclaredFields();

        for(Field field : fields) {
            Class<? extends Object> typeClass = field.getType();// 属性类型
            for(int i = 0; null != columnNames && i < columnNames.length; i++) {
                String columnName = columnNames[i];
                typeClass = getBasicClass(typeClass);
                boolean isBasicClassType = isBasicClassType(typeClass);

                if(isBasicClassType) {// 是基本类型
                    if(columnName.equalsIgnoreCase(field.getName())) {
                        String columnNameStr = cursor.getString(cursor.getColumnIndex(columnName));
                        if(TextUtils.isEmpty(columnNameStr)) {
                            break;
                        }

                        columnNameStr = TextUtils.isEmpty(columnNameStr) ? "" : columnNameStr;

                        Constructor<? extends Object> constructor = typeClass.getConstructor(String.class);
                        Object attribute = constructor.newInstance(columnNameStr);
                        field.setAccessible(true);
                        field.set(obj, attribute);
                        break;
                    }
                }else {
                    Object obj2 = setValuesToFields(cursor, typeClass);// 递归
                    field.set(obj, obj2);
                    break;
                }
            }
        }

        return obj;
    }

    static {
        basicClassMap.put(int.class, Integer.class);
        basicClassMap.put(long.class, Long.class);
        basicClassMap.put(float.class, Float.class);
        basicClassMap.put(double.class, Double.class);
        basicClassMap.put(boolean.class, Boolean.class);
        basicClassMap.put(byte.class, Byte.class);
        basicClassMap.put(short.class, Short.class);
    }
}
